package com.jing.juc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SaleTick {

    private int ticket = 50;

    private final Lock lock = new ReentrantLock();//独占锁 可重入锁 公平|非公平锁  默认为非公平

/*    public synchronized void sale(){
        if (ticket > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出第" + ticket-- + "张,还剩" + ticket + "张");
        }
    }*/

    public void sale(){
        //独占锁 可重入锁 公平|非公平锁  默认为非公平
        lock.lock();
        try {
            if (ticket > 0){
                System.out.println(Thread.currentThread().getName() + "卖出第" + ticket-- + "张,还剩" + ticket + "张");
            }
        } finally {
            lock.unlock();
        }
    }


}
